﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using HandyControl.Controls;
using WWLauncher.Interface;
using WWLauncher.Model;
using WWLauncher.Util;
using WWLauncher.View.Other;
using WWLauncher.ViewModel;
using static WWLauncher.Util.ShowWindowFollowMouse;
using TabItem = System.Windows.Controls.TabItem;
using Window = System.Windows.Window;

namespace WWLauncher.View
{
    /// <summary>
    /// IconfontWindow.xaml 的交互逻辑
    /// </summary>
    public partial class IconfontWindow : Window, IWindowCommon
    {
        private AppConfig appConfig = MainViewModel.Instance().AppConfig;
        private static MenuViewModel menuInfo;
        private List<IconfontModel> systemIcons;
        private List<IconfontModel> customIcons;
        public static IconfontViewModel vm;
        private IconfontWindow(MenuViewModel menuInfo)
        {
            InitializeComponent();
            this.Topmost = true;
            IconfontWindow.menuInfo = menuInfo;
            vm = new IconfontViewModel();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            systemIcons = SvgToGeometry.GetIconfonts();
            vm.Iconfonts = systemIcons;
            this.DataContext = vm;
        }

        /// <summary>
        /// 移动窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
       
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.DataContext = null;
            this.Close();
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabItem ti = this.MyTabControl.SelectedItem as TabItem;

            switch (ti.Tag.ToString())
            {
                case "Custom":
                    CustomButton.IsEnabled = true;
                    if (appConfig.CustomIconUrl.IsNullOrEmpty() || appConfig.CustomIconJsonUrl.IsNullOrEmpty())
                    {
                        LoadingEle.Visibility = Visibility.Visible;
                        CustomIcon.Visibility = Visibility.Collapsed;
                        HandyControl.Controls.Dialog.Show(new CustomIconUrlDialog(appConfig), "IconUrlDialog");
                    }
                    else
                    {
                        if (customIcons == null)
                        {
                            vm.Iconfonts = null;
                            LoadingOnlineIcon();
                        }
                        else
                        {
                            vm.Iconfonts = customIcons;
                            LoadingEle.Visibility = Visibility.Collapsed;
                            CustomIcon.Visibility = Visibility.Visible;
                        }
                    }
                    break;
                default:
                    if (CustomButton != null)
                    {
                        CustomButton.IsEnabled = false;
                    }
                    if (vm != null)
                    {
                        vm.Iconfonts = systemIcons;
                    }
                    break;
            }
        }

        private void Confirm_Click(object sender, RoutedEventArgs e)
        {
            TabItem ti = this.MyTabControl.SelectedItem as TabItem;
            int index;
            switch (ti.Tag.ToString())
            {
                case "Custom":
                    index = this.CustomIcon.IconListBox.SelectedIndex;
                    if (index != -1)
                    {
                        menuInfo.MenuGeometry = customIcons[index].Text;
                    }
                    break;
                default:
                    index = this.SystemIcon.IconListBox.SelectedIndex;
                    if (index != -1)
                    {
                        menuInfo.MenuGeometry = systemIcons[index].Text;
                    }
                    break;
            }
            this.DataContext = null;
            this.Close();
        }


        private static System.Windows.Window _window;
        public static void Show(MenuViewModel menuInfo)
        {
            if (_window == null || !_window.Activate())
            {
                _window = new IconfontWindow(menuInfo);
            }
            _window.Show();
            Keyboard.Focus(_window);
            ShowWindowFollowMouse.Display(_window, MousePosition.LeftCenter, 0, 0);
        }

        private void CustomButton_Click(object sender, RoutedEventArgs e)
        {
            Dialog.Show(new CustomIconUrlDialog(appConfig), "IconUrlDialog");
        }


        private async void LoadingOnlineIcon()
        {
            try
            {
                using (var net = new NetHelper())
                {
                    var svgJsRes = await net.GetAsync(appConfig.CustomIconUrl);
                    var jsonRes = await net.GetAsync(appConfig.CustomIconJsonUrl);

                    List<IconfontModel> icons;
                    if (svgJsRes.StatusCode == HttpStatusCode.OK)
                    {
                        icons = SvgToGeometry.GetIconfonts(svgJsRes.Html, jsonRes.Html);
                        customIcons = icons;
                    }
                    else
                    {
                        HandyControl.Controls.Growl.WarningGlobal("加载远程图标异常!");
                    }
                }

                vm.Iconfonts = customIcons;
                LoadingEle.Visibility = Visibility.Collapsed;
                CustomIcon.Visibility = Visibility.Visible;
            }
            catch (Exception e)
            {
                HandyControl.Controls.Growl.WarningGlobal("加载远程图标异常!");
                LogUtil.WriteErrorLog(e, "加载远程图标异常!");
            }
        }

        public void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.DataContext = null;
                this.Close();
            }
        }

        public class IconfontViewModel : INotifyPropertyChanged
        {
            private List<IconfontModel> iconfonts;
            private string isSettingUrl;

            public List<IconfontModel> Iconfonts
            {
                get
                {
                    return iconfonts;
                }
                set
                {
                    iconfonts = value;
                    OnPropertyChanged("Iconfonts");
                }
            }

            public string IsSettingUrl
            {
                get
                {
                    return isSettingUrl;
                }
                set
                {
                    isSettingUrl = value;
                    OnPropertyChanged("IsSettingUrl");
                }
            }


            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

      
    }
}